<template>
  <div class="right-chart-2">
    <div class="rc1-header">视频监控</div>
    <div class="rc1-chart-container">
      <div class="right">
        <el-carousel indicator-position="outside" trigger="click" :autoplay="false" :interval="10000" height="480px" @change="changeIndex">
          <el-carousel-item v-for="(item,index) in videoMonitors" :key="index">
            <div :id="`video-container-${index}`" style="width:540px;height:480px;" />
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
  </div>
</template>

<script>
import EZUIKit from 'ezuikit-js'
export default {
  name: 'RightChart2',
  props: {
    videoMonitors: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      currentIndex: 0,
      player: {}
    }
  },
  watch: {
    currentIndex: {
      handler(newVal, oldVal) {
        this.videoMonitors.forEach((item, index) => {
          if (index === newVal) {
            this.player = this.createPlayer(`video-container-${index}`, item.serialno, item.accessToken)
          } else {
            this.player.stop()
          }
        })
      },
      deep: true,
      immediate: true
    }
  },
  beforeDestroy() {
    this.player && this.player.stop() // 销毁并停止直播视频
  },
  methods: {
    changeIndex(e) {
      this.currentIndex = e
    },
    createPlayer(divID, videoID, token) {
      var player = new EZUIKit.EZUIKitPlayer({
        id: divID, // 视频容器ID
        accessToken: token, // 'at.095hrghi3lb6llbr1qjdjnqjdrqz817w-3k6gvgee40-1uwa2op-thzhivp8s',
        url: 'ezopen://open.ys7.com/' + videoID + '/1.live',
        template: 'security', // simple - 极简版;standard-标准版;security - 安防版(预览回放);voice-语音版；
        autoPlay: false,
        // 视频上方头部控件
        header: ['capturePicture', 'save', 'zoom'], // 如果templete参数不为simple,该字段将被覆盖
        // 视频下方底部控件
        footer: ['talk', 'broadcast', 'hd', 'fullScreen'], // 如果template参数不为simple,该字段将被覆盖
        audio: 1, // 是否默认开启声音 0 - 关闭 1 - 开启
        plugin: ['talk'], // 加载插件，talk-对讲
        // openSoundCallBack: (data) => console.log('开启声音回调', data),
        // closeSoundCallBack: (data) => console.log('关闭声音回调', data),
        // startSaveCallBack: (data) => console.log('开始录像回调', data),
        // stopSaveCallBack: (data) => console.log('录像回调', data),
        // capturePictureCallBack: (data) => console.log('截图成功回调', data),
        // fullScreenCallBack: (data) => console.log('全屏回调', data),
        // getOSDTimeCallBack: (data) => console.log('获取OSDTime回调', data),
        // handleSuccess: (data) => {
        //   console.log('播放成功回调', data)
        // },
        // handleError: (data) => console.log('播放失败回调', data),
        width: 540,
        height: 480
      })
      this.player = player
      return player
    }
  }
}
</script>

<style lang="scss">
.right-chart-2 {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .rc1-header {
    font-size: 20px;
    font-weight: bold;
    height: 30px;
    line-height: 30px;
  }

  .rc1-chart-container {
    flex: 1;
    display: flex;
    height:500px;
  }

  .left {
    width: 30%;
    font-size: 16px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .number {
      font-size: 34px;
      color: #096dd9;
      font-weight: bold;
      margin-bottom: 30px;
    }
  }

  .right {
    flex: 1;
    box-sizing: border-box;
  }

  .vjs-default-skin {
    width: 88%;
    height: 220px;
  }
}
</style>
